package com.woniuxy.controllers;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.entitys.Users;
import com.woniuxy.services.UserService;
import com.woniuxy.utils.GetMD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "员工管理接口")
public class UserController {
    private static final String SQLSTATE_23000 = "23000";

    @Autowired
    private UserService userService;

    @ApiOperation(
            value = "查询所有员工",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="条目数",required=true),
            @ApiImplicitParam(paramType="query",name="key",value="查询类型",required=false),
            @ApiImplicitParam(paramType="query",name="value",value="查询内容",required=false),
    })
    //根据查询语句查询全部，分页显示
    @GetMapping("/users/key")
    public RestPOJO getUsers(Integer currentPage,Integer pageSize,String key ,String value){

        //System.out.println(currentPage+"$$$" +pageSize +"%%%%"+ key + "####" + value);
        PageInfo<Users>  users =userService.getUsersByKey(currentPage,pageSize,key,value);
        System.out.println(users);
        RestPOJO restPOJO = new RestPOJO();
        if(users != null && users.getList().size()>0){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(users);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "登录",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="user_tel",value="电话",required=true),
            @ApiImplicitParam(paramType="query",name="user_password",value="密码",required=true),
    })
    //    根据电话和密码登录
    @GetMapping("/users")
    public RestPOJO login(String user_tel ,String user_password){
        //System.out.println(user_tel);
        //System.out.println(user_password);
        Users user = userService.login(user_tel,user_password);
        RestPOJO restPOJO = new RestPOJO();
        if(user != null){
            restPOJO.setMessage("登录成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(user);

        }else{
            restPOJO.setMessage("登录失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;

    }



    @ApiOperation(
            value = "根据员工id查询",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="user_id",value="员工id",required=true),
    })
    //    根据user_id查询
    @GetMapping("/users/{user_id}")
    public RestPOJO getUserByUid(@PathVariable("user_id") Integer user_id){
        System.out.println(user_id);
        Users user = userService.getUserByUid(user_id);
        RestPOJO restPOJO = new RestPOJO();
        if(user != null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(user);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;

    }


    @ApiOperation(
            value = "查询所有在职客户经理的id和name",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //查询所有在职客户经理的id和name
    @GetMapping("/users/manager")
    public RestPOJO getAllManager(){
        List<Users> usersList = userService.getAllManager();
        RestPOJO restPOJO = new RestPOJO();
        if(usersList != null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(usersList);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询所有员工",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    // 查询全部
    @GetMapping("/users/all")
    public RestPOJO getAllUsers(){
        List<Users> usersList = userService.getAllUsers();
        RestPOJO restPOJO = new RestPOJO();
        if(usersList != null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(usersList);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;

    }

    @ApiOperation(
            value = "保存修改的员工信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存(删除1个)修改员工
    @PutMapping("/users")
    public RestPOJO saveUpdUser(@RequestBody Users users){
        //System.out.println(users);
        RestPOJO restPOJO = null;
        try {
            restPOJO = new RestPOJO();
            Integer  count = userService.updUser(users);
            if(count > 0){
                restPOJO.setMessage("修改成功");
                restPOJO.setStatus(1000);

            }else{
                restPOJO.setMessage("修改失败");
                restPOJO.setStatus(1001);
            }
        } catch (Exception e) {
                e.printStackTrace();
                restPOJO.setMessage("修改失败！员工编码不能重复");
                restPOJO.setStatus(1002);
        }



        return restPOJO;
    }

    @ApiOperation(
            value = "批量修改员工状态,数据格式如下[88,99]",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //批量删除（修改）员工
    @PostMapping("/users/deletelist")
    public RestPOJO updSomeUsers(@RequestBody Object[] checkedIds){
        System.out.println(checkedIds);

        Integer count = userService.updSomeUsers(checkedIds) ;
        RestPOJO restPOJO = new RestPOJO();
        if(count > 0){
            restPOJO.setMessage("批量删除成功");
            restPOJO.setStatus(1000);

        }else{
            restPOJO.setMessage("批量删除失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "保存添加员工",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //保存添加员工
    @PostMapping("/users")
    public RestPOJO saveAddUser(@RequestBody Users users){
        RestPOJO restPOJO = null;
        try {
            restPOJO = new RestPOJO();
            //默认密码123456，默认在职
            users.setUser_status(1);
            users.setUser_password(GetMD5.getMD5Str("123456"));
            Integer count = userService.addUser(users) ;

            if(count > 0){
                restPOJO.setMessage("添加成功");
                restPOJO.setStatus(1000);

            }else{
                restPOJO.setMessage("添加失败");
                restPOJO.setStatus(1001);
            }
        } catch (Exception e) {
            //System.out.println("@@@@@@@@@@@@@@@@@@@@@");
            e.printStackTrace();
            restPOJO.setMessage("添加失败！员工编码不能重复。");
            restPOJO.setStatus(1002);
            //System.out.println("@@@@@@@@@@@@@@@@@@@@@"+restPOJO);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询所有在职员工",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    //查询所有在职员工
    @GetMapping("/users/selwork")
    public RestPOJO getAllWorkUser(){
        List<Users> usersList = userService.getAllWorkUser();
        RestPOJO restPOJO = new RestPOJO();
        if(usersList != null){
            restPOJO.setMessage("查询成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(usersList);

        }else{
            restPOJO.setMessage("查询失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;

    }

}
